// pages/bCount/bCount.js
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // array: [{
    //     'day': '2020-05-08',
    //     'balance': '1000.1'
    //   },
    //   {
    //     'day': '2020-05-05',
    //     'balance': '51'
    //   },
    //   {
    //     'day': '2020-05-01',
    //     'balance': '51'
    //   }
    // ],
    array: [ ],
    start_time: util.formatDate(new Date()),
    end_time: util.formatDate(new Date()),
    date: util.formatDate(new Date()),
    ye: '',
    currentData: 0,
    clientHeight:'',
    sum: 0
  },

  st_bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      start_time: e.detail.value
    })
  },

  et_bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      end_time: e.detail.value
    })
  },

  onConfirm: function(e){
    var json = {};
    if(this.data.ye == ''){
      wx.showToast({
        title: '余额填写不对',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    json["day"] = this.data.date;
    json["balance"] = this.data.ye;
    var arr = this.data.array;
    arr.push(json);
    this.setData({
      array:arr
    });
  },

  onCancel: function(e){
    this.setData({
      array:[]
    });
  },

  //点击切换，滑块index赋值
  checkCurrent: function (e) {
    const that = this;
    if (that.data.currentData === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentData: e.target.dataset.current
      })
    };
  },

    //获取当前滑块的index
    bindchange: function (e) {
      const that = this;
      that.setData({
        currentData: e.detail.current
      })
    },

  input_ye: function (e) {
    this.setData({
      ye: e.detail.value
    });
  },

  rq_bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      date: e.detail.value
    })
  },

  getSum: function () {
    var that = this;
    var arr = {};
    var sDay = that.data.start_time;
    var eDay = that.data.end_time;

    //开始时间没值不行
    var timestamp = new Date(sDay).getTime();
    var rDay = new Date(timestamp);
    var rStr = util.formatDate(rDay);

    that.data.array.forEach(function (item, index) {
      var t1 = new Date(sDay).getTime();
      var t2 = new Date(item.day).getTime();
      if (t1 > t2) {
        sDay = item.day;
      }
      console.log(sDay);
      arr[item.day] = item.balance;
    });
    console.log(arr);

    if(!arr.hasOwnProperty(rStr)){
      wx.showToast({
        title: '开始日期不能没有余额',
        icon: 'none',
        duration: 1000
      });
      return;
    }

    var rbalance = 0;
    var sum = 0;
    var i = 0;
    while (eDay != rStr && i < 600) {
      var ts = i == 0 ? new Date(rDay).getTime() : new Date(rDay).getTime() + 24 * 60 * 60 * 1000;
      var d = new Date(ts);
      var dateTime = d.toLocaleString();
      console.log(dateTime);
      rStr = util.formatDate(d);
      rDay = d;
      console.log(rStr);

      if (arr.hasOwnProperty(rStr)) {
        rbalance = parseFloat(arr[rStr]);
      }
      sum = sum + rbalance;
      console.log(sum);
      i++;
    }

    that.setData({
      sum: sum
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          clientHeight: res.windowHeight-177
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})